package com.glory.cqaospda.view

import android.content.Context
import android.os.Bundle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.cqaospda.R
import com.glory.cqaospda.adapter.TestMachineAdapter
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.bean.BaseConfigBean
import com.glory.cqaospda.bean.TestMachineBean
import com.glory.cqaospda.viewmodel.LoadViewModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.glorysoft.hlgdpda.utils.PopWindowsUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils
import kotlinx.android.synthetic.main.activity_load.*
import kotlinx.android.synthetic.main.activity_title.*

class LoadActivity : BaseActivity<LoadViewModel>() {

    private var testMachinePop: PopWindowsUtils.GeneralPop? = null
    private var typeList = ArrayList<String>()
    private var testMachineList = ArrayList<TestMachineBean>()
    var adapter: TestMachineAdapter? = null

    override fun initData() {

        viewModel.defaultGetBaseConfig()

        viewModel.defUI.callObserve.observe(this@LoadActivity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        val baseConfigBean = it.item as BaseConfigBean
                        for (item in baseConfigBean.mItemList) {
                            typeList.add(item.mItemValue)
                        }
                    }
                    2 -> {
                        viewModel.defUI.toastEvent.value = ToastMessageBean("重新加载成功")
                        finish()
                    }
                }
            } else {
                Constant.okgoError = false
            }
        }

        flTestMachine.setOnClickListener{
            if (typeList.size == 0){
                viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到测试机类型")
                return@setOnClickListener
            }
            testMachinePop = PopWindowsUtils.getOneChoosePop(this, flTestMachine.width, typeList,
                ivTypeArrow, object : PopWindowsUtils.PopTextAndPositionCallback {
                    override fun onSuccess(string: String, potion: Int) {
                        tvTestMachineType.text = string
                        val tm = TestMachineBean()
                        tm.testerType = string
                        FreeApi_LogUtils.saveErrorMessages(adapter!!.data.toString(), "adapter")
                        FreeApi_LogUtils.saveErrorMessages(tm.testerType, "testerType")
                        FreeApi_LogUtils.saveErrorMessages(tm.toString(), "TestMachineBean")
                        if(isTestMachine(tm)){
                            testMachineList.add(tm)
                            adapter!!.setNewData(testMachineList)
                            adapter!!.notifyDataSetChanged()
                        }
                    }
                }
            )
            if (testMachinePop!!.isShowing) {
                testMachinePop?.dismiss()
            } else {
                PopWindowsUtils.setArrowShow(ivTypeArrow)
                testMachinePop?.showAsDropDown(flTestMachine, 0, 0)
            }
        }

        tvBaseSubmit.setOnClickListener{
            if (Constant.isFastClick()) {
                if (etEqpId.text.toString() == "") {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请扫描设备ID", false)
                    return@setOnClickListener
                }
                if (etLotId.text.toString() == "") {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请扫描批次ID", false)
                    return@setOnClickListener
                }
                if (adapter!!.data.size == 0) {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("设备类型至少选择一条", false)
                    return@setOnClickListener
                }
                val map = JsonObject()
                map.addProperty("LotNo", etLotId.text.toString())
                map.addProperty("EquipmentName", etEqpId.text.toString())
                map.add(
                    "TesterTypeList",
                    JsonParser().parse(Gson().toJson(adapter!!.data)).asJsonArray
                )
                viewModel.defaultProgramReload(map)
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        tvTitle.text = "重新加载测试程序"
        tvUserName.text = Constant.UserName
        adapter = TestMachineAdapter(null)
        initRecyclerView(rvTestMachineList, this)
        rvTestMachineList.adapter = adapter
    }

    override fun layoutId(): Int {
        return R.layout.activity_load
    }

    protected fun initRecyclerView(recyclerView: RecyclerView, context: Context?) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(DividerItemDecoration(context, 1))
    }

    private fun isTestMachine(response: TestMachineBean): Boolean {
        if (adapter!!.data.size <= 0) {
            return true
        }
        for (entity in adapter!!.data) {
            if (entity.testerType == response.testerType) {
                viewModel.defUI.toastEvent.value =
                    ToastMessageBean("当前类型以添加到列表", false)
                return false
            }
        }
        return true
    }
}